package edu.szu.huhanpeng.doodlingFight.controller.dataController.gameStart 
{
	import edu.szu.huhanpeng.doodlingFight.globalConst.GlobalConst;
	import edu.szu.huhanpeng.doodlingFight.interfaces.IChessBoardData;
	import edu.szu.huhanpeng.doodlingFight.interfaces.IChessData;
	import edu.szu.huhanpeng.doodlingFight.interfaces.ISquareData;
	import edu.szu.huhanpeng.doodlingFight.model.ChessBoardProxy;
	import edu.szu.huhanpeng.doodlingFight.model.ChessProxy;
	import edu.szu.huhanpeng.doodlingFight.model.data.ChessData;
	import edu.szu.huhanpeng.doodlingFight.view.ChessBoardMed;
	import edu.szu.huhanpeng.doodlingFight.view.ChessMediator;
	import edu.szu.huhanpeng.doodlingFight.view.components.ChessComponent;
	import org.puremvc.as3.interfaces.ICommand;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.interfaces.INotifier;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	/**
	 * 往棋盘上重新安放所有棋子。即把棋子数据，棋盘上方格数据，棋子的显示对象关联起来
	 * @author Huhanpeng
	 */
	public class InitChessesDc extends SimpleCommand implements ICommand, INotifier 
	{
		
		override public function execute(note:INotification):void 
		{
			var chessBoardProxy:ChessBoardProxy = facade.retrieveProxy( ChessBoardProxy.NAME ) as ChessBoardProxy;
			var chessBoardMed:ChessBoardMed = facade.retrieveMediator( ChessBoardMed.NAME ) as ChessBoardMed;
			
			var allSquare:Vector.<ISquareData> = chessBoardProxy.allSquare;
			
			//棋子的component，mediatordata,proxy，都用同一个name,由data创建，是每个棋子的独立标识
			var squareData:ISquareData;
						//  0  1  2  3  4  5  6  7  8  9
			var arr:Array =[5, 5, 5, 1, 3, 2, 3, 4, 4, 5,//0
							1, 4, 5, 3, 2, 4, 1, 5, 5, 5,//1
							5, 2, 2, 3, 5, 4, 3, 2, 4, 4,//2
							5, 1, 3, 1, 4, 1, 3, 2, 5, 4,//3
							4, 5, 1, 1, 1, 5, 1, 3, 4, 3,//4
							3, 4, 3, 5, 2, 3, 1, 3, 5, 3,//5
							3, 5, 1, 2, 4, 1, 5, 3, 1, 3,//6
							1, 3, 1, 4, 2, 4, 3, 2, 2, 2,//7
							3, 3, 4, 2, 2, 5, 3, 3, 5, 5,//8
							1, 3, 5, 5, 2, 4, 4, 5, 2, 2 //9
							];
			
			var chessData:IChessData;
			var chessComponent:ChessComponent;
			for (var i:int = 0; i < allSquare.length; i++) 
			{
				//创建棋子数据，关联棋盘方格与棋子数据
				squareData = allSquare[i];
				chessData = new ChessData();
				facade.registerProxy(new ChessProxy(chessData));
				squareData.placeChess(chessData);
				
				
				chessData.type = Math.floor(Math.random() * GlobalConst.setting.CHESS_TYPE) + 1;
				//chessData.type = arr[i];
				
				chessData.updatePosData(squareData.row, squareData.column);
				
				//创建棋子显示对象，关联棋子数据与显示对象
				chessComponent = new ChessComponent(chessData.type);
				chessComponent.name = chessData.name
				facade.registerMediator(new ChessMediator(chessComponent));
				
				chessComponent.x = chessData.x;
				chessComponent.y = chessData.y;
				chessComponent.addEvent();
				chessBoardMed.addChess( chessComponent );
				//sendNotification(GlobalConst.noteName.STAGE_ADD_CHILD, chessData.name);
			}
		}
	}

}